package com.example.xm.adapter.HomeAdapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.bumptech.glide.Glide;
import com.example.xm.R;
import com.example.xm.base.BaseAdapter;
import com.example.xm.bean.HomeBean.HomeYouHomeExerciseBean;

import java.util.List;

public class YouHomeExerciseAdapter extends BaseAdapter {
    public YouHomeExerciseAdapter(Context context, List data) {
        super(context, data);
    }

    @Override
    protected int getLayout() {
        return R.layout.you_home_exercise_item;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @SuppressLint("SetTextI18n")
    @Override
    protected void bindData(BaseViewHolder viewHolder, Object o) {
        ImageView img = (ImageView) viewHolder.getViewById(R.id.iv_head_exercise);
        TextView yijieshu = (TextView) viewHolder.getViewById(R.id.tv_yijieshu);
        TextView title = (TextView) viewHolder.getViewById(R.id.tv_title_exercise);
        LinearLayout tags = (LinearLayout) viewHolder.getViewById(R.id.layout_tags);
        TextView date = (TextView) viewHolder.getViewById(R.id.tv_date_exercise);
        TextView baoming = (TextView) viewHolder.getViewById(R.id.tv_baoming);
        TextView dizhi = (TextView) viewHolder.getViewById(R.id.tv_dizhi);
        HomeYouHomeExerciseBean.DataBean.ActivitysBean bean = (HomeYouHomeExerciseBean.DataBean.ActivitysBean) o;
        Glide.with(context).load(bean.getCover()).into(img);
        title.setText(bean.getTitle());
        baoming.setText("已报名：" + bean.getApplyUserCount());
        dizhi.setText(bean.getLocation());
        date.setText(bean.getStartTime());
        //清理布局中的所有子元素
        tags.removeAllViews();
        //动态添加
        for (HomeYouHomeExerciseBean.DataBean.ActivitysBean.ColorTagsBean item:bean.getColorTags()){
            TextView txt = (TextView) LayoutInflater.from(context).inflate(R.layout.layout_tag_status,null);
            txt.setText(item.getTagName());
            txt.setTextColor(Color.parseColor(item.getTagColor()));
            tags.addView(txt);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMarginEnd(10);
            txt.setLayoutParams(params);
            //动态修改边框的颜色
            GradientDrawable gradientDrawable = (GradientDrawable) txt.getBackground();
            gradientDrawable.setStroke(2,Color.parseColor(item.getTagColor()));
        }
    }
}
